package com.lyb.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.lyb.spzx.manager.service.ProductSpecService;
import com.lyb.spzx.model.entity.product.ProductSpec;
import com.lyb.spzx.model.vo.common.Result;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品规格接口")
@RestController
@RequestMapping("admin/product/productSpec")
public class ProductSpecController {

    @Autowired
    ProductSpecService productSpecService;

    @Operation(summary = "分页查找")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageInfo<ProductSpec>> findSpecByPage(@PathVariable Integer pageNum,
                                                        @PathVariable Integer pageSize){
        PageInfo<ProductSpec> specPageInfo = productSpecService.findById(pageNum,pageSize);
        return Result.build(specPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加规格")
    @PostMapping("/saveSpec")
    public Result saveSpec(@RequestBody ProductSpec productSpec){
        productSpecService.svaeSpec(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改规格")
    @PutMapping("/updateSpec")
    public Result updateSpec(@RequestBody ProductSpec productSpec){
        productSpecService.updateSpec(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除规格")
    @DeleteMapping("/deleteSpec/{id}")
    public Result deleteSpec(@PathVariable Long id){
        productSpecService.deleteSpec(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询所有规格")
    @GetMapping("/findAllSpec")
    public Result<List<ProductSpec>> findAllSpec(){
        List<ProductSpec> productSpecs = productSpecService.findAllSpec();
        return Result.build(productSpecs,ResultCodeEnum.SUCCESS);
    }
}
